package com.ailink.basic.thread;

import java.util.concurrent.locks.ReentrantLock;

public class TestThread {
    private static Object LOCK = new Object();

    private static void runWait() {
        synchronized (LOCK) {
            try {
                LOCK.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized static void runSynchronized() {
        try {
            Thread.sleep(1000 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static ReentrantLock reentrantLock = new  ReentrantLock();

    private static void runPark() {
        try {
            reentrantLock.lock();
            Thread.sleep(1000 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                runSynchronized();
            }, "runSynchronized " + i).start();
        }

        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                runPark();
            }, "runPark " + i).start();
        }

        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                runWait();
            }, "runWait " + i).start();
        }

        try {
            Thread.sleep(1000 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
